package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import xyz.hubery.domain.CoinType;
import xyz.hubery.model.R;
import xyz.hubery.service.CoinTypeService;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/coinTypes")
@Api("币种类型")
public class FinanceController {

    @Autowired
    private CoinTypeService coinTypeService;

    @GetMapping
    @ApiOperation(value = "分页查询币种类型")
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<Page<CoinType>> findPage(@ApiIgnore Page<CoinType> page, String code) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<CoinType> coinTypePage = coinTypeService.findPage(page, code);
        return R.ok(coinTypePage);
    }


    @PostMapping
    @ApiOperation(value = "新增币种类型")
    @PreAuthorize("hasAuthority('trade_coin_type_create')")
    public R<Boolean> add(@RequestBody @Validated CoinType coinType) {
        boolean save = coinTypeService.save(coinType);
        return R.ok(save);
    }

    @PatchMapping
    @ApiOperation(value = "修改货币类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinType" ,value = "coinType 的 json")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R update(@RequestBody @Validated CoinType coinType){
        boolean update = coinTypeService.updateById(coinType);
        if(update){
            return R.ok() ;
        }
        return R.fail("修改失败") ;
    }

    @PostMapping("/setStatus")
    @ApiOperation("修改币种类型状态")
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R setStatus(@RequestBody CoinType coinType){
        boolean update = coinTypeService.updateById(coinType);
        if(update){
            return R.ok() ;
        }
        return R.fail("修改失败") ;
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有的币种类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status",value = "币种的状态")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<List<CoinType>> findAllCoinTypeByStatus(Byte status){
        List<CoinType> coinTypes = coinTypeService.listByStatus(status) ;
        return R.ok(coinTypes) ;
    }

}
